﻿using ApiCoreProject.Dal;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;

namespace ApiCoreProject.Bll
{
    public class BaseBll<T> : IBaseBll<T> where T : class, new()
    {
        protected IBaseDal<T> dal;

        public BaseBll(IBaseDal<T> dal){
            this.dal = dal;
        }


        public T Add(T t,bool isSave)
        {
            dal.Add(t);
            if (isSave) {
                if (SaveChanges() > 0) {
                    return t;
                }
            }
            return null;
        }

        public void Delete(T t,bool isSave)
        {
            dal.Delete(t);
            if (isSave)
            {
                SaveChanges();
            }
        }

        public void DeleteBatch(List<T> list, bool isSave)
        {
            dal.DeleteBatch(list);
            if (isSave)
            {
                SaveChanges();
            }
        }

        public void Update(T t, bool isSave)
        {
            dal.Update(t);
            if (isSave)
            {
                SaveChanges();
            }
        }

        public IQueryable<T> GetModels(Expression<Func<T, bool>> whereLambda)
        {
            return dal.GetModels(whereLambda);
        }

        public IQueryable<T> GetModelsByPage<type>(int pageSize, int pageIndex, bool isAsc, Expression<Func<T, type>> orderByLambda, Expression<Func<T, bool>> whereLambda)
        {
            return dal.GetModelsByPage(pageSize, pageIndex, isAsc, orderByLambda, whereLambda);
        }

        public int SaveChanges()
        {
             return dal.SaveChanges();
        }

        
    }
}
